<?php

if(isset($_POST['daterange']))
{	 
	$_POST['daterange'];
	$filterdates = split('to', $_POST['daterange']);
	if ( count($filterdates)<2)
	{
		$filterdates[0]=$_POST['daterange'];
		$filterdates[1]=$_POST['daterange'];
	}
}
else 
{
	$filterdates[0]=date("Y-m-d", strtotime(date('Y').'-'.date('m').'-01')) ;
	$filterdates[1]=date("Y-m-d");
	//print_r($filterdates);
}


$cdataProvider=new CActiveDataProvider('Payments', array(
			'criteria'=>array(
			'condition'=>'idIncomeType IN (select id from Incometype where Category in( "Payment" , "Income","Expense","Security Deposits")) AND PaidDate BETWEEN \''.$filterdates[0].'\' and \''.$filterdates[1].'\'',
			'with'=>array('idOccupancy0'=>array(
   		    'select'=>'id,idRentalProperty',
    		'condition'=>'idOccupancy0.idRentalProperty='.$pid,	
   			 ),
   			 'idOccupancy0.idTenant0.iduser0'=>array(
   			 'select'=>'FirstName , LastName'
   			 
   			 ),
   			 'idOccupancy0.idTenant0'=>array(
   			 'select'=>'id'
   			 
   			 ),
   			
   			 ),
   			 
			),
			'pagination'=>false,   
		));
	
		
   

   $itotal=0;
   $etotal=0;
   $btotal=0;
   
    	
    	$_minus = array('Returned','Expense','Fee');
	   foreach($cdataProvider->data as $item)
	    { 
	    	
	    	if( (!in_array($item->idIncomeType0->Name, $_minus)) && (!in_array($item->idIncomeType0->Category, $_minus)) )
	    	{
	    		$itotal += $item->Amount;
	    		$btotal += $item->Amount;
	    		 
	    	}
			else 
			{
				$etotal += $item->Amount; 
				$btotal-=$item->Amount;  	
			}
					    	

	    }
	    
  
   
   	Yii::import('zii.widgets.grid.CGridColumn');
 
   	class FullNameColumn extends CGridColumn {
		 
		    		   	
		   public function renderDataCellContent($row, $data) { // $row number is ignored
		  		
		    	 		
		        echo CHtml::link($data->idOccupancy0->idTenant0->iduser0->FirstName . ' '.$data->idOccupancy0->idTenant0->iduser0->LastName,array('/adminpanel/tenant/view','id'=>$data->idOccupancy0->idTenant0->id));
		        
		    }
		    
		   
			
		  
		}
   	
class BalanceColumn extends CGridColumn {
		 
		    private  $_total = 0;
		   	
		   public function renderDataCellContent($row, $data) { // $row number is ignored
		  		
		    	$_minus = array('Returned','Expense','Fee');
		    	
		    	if(in_array($data->idIncomeType0->Name, $_minus) || in_array($data->idIncomeType0->Category, $_minus))
		    	$this->_total-=$data->Amount;
		    	else
		    	$this->_total += $data->Amount;
		 		
		        echo $this->_total;
		        
		    }
		    
		   
			
		  
		}
	
   
   class IncomeColumn extends CGridColumn {
		 
		    private  $_total = 0;
		   	
		   public function renderDataCellContent($row, $data) { // $row number is ignored
		  		    	
		    //if($data->idIncomeType0->Category == 'Income' || $data->idIncomeType0->Category == 'Payment')
			$_minus = array('Returned','Expense','Fee');
		    	
		    	if((!in_array($data->idIncomeType0->Name, $_minus)) && (!in_array($data->idIncomeType0->Category, $_minus)))
		   		echo $data->Amount;
		        
		    }
		  
		}
		
		class ExpenseColumn extends CGridColumn {
		 
		    private  $_total = 0;
		   	
		   public function renderDataCellContent($row, $data) { // $row number is ignored
		  		    	
		    if($data->idIncomeType0->Category == 'Expense')
		        echo $data->Amount;
		        
		    }
		  
		}	


	
?>




<?php


$form=$this->beginWidget('CActiveForm', array(
	'id'=>'user-form',
	'enableAjaxValidation'=>false,
	
));
echo '<table border=0> <tr> <td width="116px">';
echo CHtml::label('Choose Date Range', 'daterange');
echo'</td><td aling="left" width="167px">';
$this->widget('ext.dateRangeSelector.SDateRange',array(
    'name'=>'daterange',
    'value'=>$filterdates[0].' to '.$filterdates[1],
    'options'=>array(
				'posY'=>'255px',
				'posX'=>'190px',
    			'rangeSplitter'=> 'to',
    			'arrows'=>true,
				'dateFormat'=>'yy-mm-dd',
				'datepickerOptions'=>array('changeMonth'=>true, 'changeYear'=>true),
				
				),
	
));
echo '</td> <td aling="left" >';
 echo CHtml::submitButton('Display');
 echo '</td> </tr> </table>';
$this->endWidget();
?>

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'paymentsfee-grid',
	'dataProvider'=>$cdataProvider,
	
	'columns'=>array(
		array(            
            'name'=> 'PaidDate',
			'header'=>'Date',
		
		
        ),
       array(            
            'header'=>'Name',
			'class'=>'FullNameColumn',
		
        ),
        array(            
            'name'=> 'idIncomeType',
			'header'=>'Type',
			'type'=>'raw',
			'value'=>'$data->idIncomeType0->Name ',
        ),
		array(            
            'header'=> 'Income',
			'class'=>'IncomeColumn',
			'footer' => $itotal,
        	'footerHtmlOptions' => array('class'=>'tblfooter'),
        ),
        array(            
            'header'=> 'Expense',
			'class'=>'ExpenseColumn',
			'footer' => $etotal,
        	'footerHtmlOptions' => array('class'=>'tblfooter'),
        ),
		
		
        array(
            'header' => 'Balance',
            'class'  => 'BalanceColumn',
        	'footer' => $btotal,
        	'footerHtmlOptions' => array('class'=>'tblfooter'),
            
        ),
	  
		array(
			'class'=>'CButtonColumn',
    'template'=>'{view} {update} {delete}',
    'buttons'=>array
    (
        'view' => array
        (
            'label'=>'View Detail',
            'imageUrl'=>Yii::app()->request->baseUrl.'/assets/a99e585a/gridview/view.png',
            'url'=>'Yii::app()->createUrl("/adminpanel/payments/view", array("id"=>$data->id,"typ"=>$data->idIncomeType0->Category, "rurl"=>Yii::app()->request->requestUri))',
        ),
        'update' => array
        (
            'label'=>'Update Detail',
            'imageUrl'=>Yii::app()->request->baseUrl.'/assets/a99e585a/gridview/update.png',
            'url'=>'Yii::app()->createUrl("adminpanel/payments/update", array("id"=>$data->id,"typ"=>$data->idIncomeType0->Category , "rurl"=>Yii::app()->request->requestUri))',
            
        ),
		
		'delete' => array
        (
            'label'=>'Delete',
            'imageUrl'=>Yii::app()->request->baseUrl.'/assets/a99e585a/gridview/delete.png',
            'url'=>'Yii::app()->createUrl("adminpanel/payments/delete", array("id"=>$data->id))',
            
        ),
		
    ),
		),
		
	),
	
)); 


?>
